﻿//refreshed the page - takes to return url if there is one
function refreshPage() {
    var curUrl = window.location.toString();
    if (curUrl.indexOf("ReturnUrl") != -1) {
        var start = curUrl.indexOf("ReturnUrl");        
        var redirectUrl = curUrl.substring(curUrl.indexOf("/", start));
        window.location = "http://" + window.location.host + redirectUrl;
    }
    else {
        window.location = curUrl + "?nocache=" + Math.random();
    }
}

//ask for permissions for a new user
function setPermissionsForNewUser() {
    FB.ensureInit(function() {
        //permissions status array
        var permissions = new Array();
        //request publish stream permission
        FB.Connect.showPermissionDialog("publish_stream", function(s) {
            //make we have a success reponse
            if (s != null && s.length)
                permissions.push(s);
            //request for email permission
            FB.Connect.showPermissionDialog("email", function(s) {
                //make sure we have a success reponse
                if (s != null && s.length)
                    permissions.push(s);

                //update our db for permissions
                if (permissions.length > 0) {
                    var fbUserId = $("input[name='fbUserId']").val();
                    //set ajax request to update permission status
                    $.getJSON("/Account/UpdatePermissionStatus/" + fbUserId + "?status=" + permissions.join(","));
                }
            });
        });
    });
}

function facebookdo() {
    var tattach = {
        "name": "Some new article",
        "href": "http://manazra.com",
        "description": "Adeel posted a new article at Manazra.com"
    };
    var tdata = { 
        "actor": "adeel",
        "site-url": "http://www.google.com",
        "article-url": "http://www.google.com",
        "article-title": "Some new article",
        "article-intro": "Some article intro something about something and more something"
    };

    FB.Connect.streamPublish(null, tattach, null, 0, 'prompt text', null, true);
    return false;
}